package com.store.app.database;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DatabaseMethod {
	private Database database;
	public DatabaseMethod(Database database) {
		this.database =database;
	}
	public static long add(Database database,long account,String password,long memberid,String memberkey
			,String trade_name,String linkman,String first_time) {
		SQLiteDatabase db = database.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("account", account);
		values.put("password", password);
		values.put("memberid", memberid);
		values.put("memberkey", memberkey);
		values.put("trade_name", trade_name);
		values.put("linkman",linkman);
		values.put("first_time",first_time);
		long id = db.insert("member", null, values);
		db.close();
		return id;
	}
	public static long addrecord(Database database,String money,String time,String card_number,String time_sign
			,String save_states) {
		SQLiteDatabase db = database.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("money", money);
		values.put("time", time);
		values.put("card_number", card_number);
		values.put("time_sign", time_sign);
		values.put("save_states", save_states);
		long id = db.insert("record", null, values);
		db.close();
		return id;
	}
	public static long add_card(Database database,long card_id,String card_name,String card_number) {
		SQLiteDatabase db = database.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("card_id", card_id);
		values.put("card_name", card_name);
		values.put("card_number", card_number);
		long id = db.insert("card", null, values);
		db.close();
		return id;
	}
	public void addstr(String table, String field, String data) {
		SQLiteDatabase db = database.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put(field, data);
		long id = db.insert(table, null, values);
		db.close();

	}
	/*
	 */
	public void addtoken(int mid,String user_token,String app_token, String security_code,String account,String password,String member_id) {
		SQLiteDatabase db = database.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("mid",mid);
		values.put("app_token", app_token);
		values.put("user_token", user_token);
		values.put("security_code", security_code);
		values.put("account", account);
		values.put("password", password);
		values.put("member_id", member_id);
		long id = db.insert("member", null, values);
		db.close();

	}
	public void update_privilege(String subtract,String subtractdata
			,String reach_amount,String reach_amountdata,String reduce_number,String reduce_numberdata) {
		SQLiteDatabase db = database.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put(subtract,subtractdata);
		values.put(reach_amount,reach_amountdata);
		values.put(reduce_number,reduce_numberdata);
		String[] parms = {"0"};
		int heartrate = db.update("special", values, "mid=?", parms);
		db.close();

	}
	public void update_site(String table,String field,String data) {
		SQLiteDatabase db = database.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put(field,data);
		String[] parms = {"0"};
		int heartrate = db.update(table, values, "mid=?", parms);
		db.close();

	}
	public void update_token(String app_token,String security_code) {
		SQLiteDatabase db = database.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("app_token", app_token);
		values.put("security_code", security_code);
		String[] parms = {"0"};
		int heartrate = db.update("member", values, "id=?", parms);
		db.close();

	}
	public void update_data(String user_token,String app_token,String security_code,String member_id,String mobile) {
		SQLiteDatabase db = database.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("user_token", user_token);
		values.put("app_token", app_token);
		values.put("security_code", security_code);
		values.put("member_id", member_id);
		values.put("mobile", mobile);
		String[] parms = {"0"};
		int heartrate = db.update("member", values, "mid=?", parms);
		db.close();

	}
	/*
	 */
	public static int upbalance(Database database, String table, String memberid,
								String trade_name,String linkman) {
		SQLiteDatabase db = database.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put("trade_name",trade_name);
		values.put("linkman",linkman);

		String[] parms = { memberid };
		int heartrate = db.update(table, values, "memberid=?", parms);
		db.close();
		return heartrate;
	}
	/*
	 * 删除一条记录
	 */
	public static void delete(Database shujuku) {

		SQLiteDatabase db = shujuku.getWritableDatabase();
		db.delete("member", "id=?", new String[] {"1"});
		db.close();
		return;
	}


	public String date() {
		SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd HH");
		String date = sDateFormat.format(new java.util.Date());
		return date;
	}



	public  List<String> getvista(String table,String field) {
		SQLiteDatabase db = database.getWritableDatabase();
		Cursor cur = db.query(table,new String[] {field}, null, null, null,
				null, null);
		List<String> returndata = new ArrayList<String>();
		for (cur.moveToFirst(); !cur.isAfterLast(); cur.moveToNext()) {
			returndata.add(cur.getString(0));
		}
		return returndata;
	}

	public List<String> gettoken() {
		SQLiteDatabase db = database.getWritableDatabase();
		Cursor cur = db.query("member",
				new String[] {"account","password"}, null, null, null,
				null, null);
		List<String> returndata = new ArrayList<String>();
		for (cur.moveToFirst(); !cur.isAfterLast(); cur.moveToNext()) {
			returndata.add(cur.getString(0));
			returndata.add(cur.getString(1));
		}
		return returndata;
	}

	public  List<String>  Local_Address() {
		SQLiteDatabase db = database.getWritableDatabase();
		Cursor cur = db.query("thisimages",
				new String[] {"posterone","postertwo","posterthree","posterfour","posterfive"}, null, null, null,
				null, null);
		List<String> returndata = new ArrayList<String>();
		for (cur.moveToFirst(); !cur.isAfterLast(); cur.moveToNext()) {
			returndata.add(cur.getString(0));
			returndata.add(cur.getString(1));
			returndata.add(cur.getString(2));
			returndata.add(cur.getString(3));
			returndata.add(cur.getString(4));
		}
		return returndata;
	}

	public  List<String> privilege() {
		SQLiteDatabase db = database.getWritableDatabase();
		Cursor cur = db.query("special",
				new String[] {"subtract","reach_amount","reduce_number","present","conditions_present","given_number"}, null, null, null,
				null, null);
		List<String> returndata = new ArrayList<String>();
		for (cur.moveToFirst(); !cur.isAfterLast(); cur.moveToNext()) {
			returndata.add(cur.getString(0));
			returndata.add(cur.getString(1));
			returndata.add(cur.getString(2));
			returndata.add(cur.getString(3));
			returndata.add(cur.getString(4));
			returndata.add(cur.getString(5));
//			Log.i("mylog",cur.getString(0)+"+"+cur.getString(1)+
//					cur.getString(2)+"+"+cur.getString(3)+
//					cur.getString(4)+"+"+cur.getString(5));
		}
		return returndata;
	}
	public  List<String> getData() {
		SQLiteDatabase db = database.getWritableDatabase();
		Cursor cur = db.query("member",
				new String[] {"account","password","member_id","user_token","security_code"}, null, null, null,
				null, null);
		List<String> returndata = new ArrayList<String>();
		for (cur.moveToFirst(); !cur.isAfterLast(); cur.moveToNext()) {
			returndata.add(cur.getString(0));
			returndata.add(cur.getString(1));
			returndata.add(cur.getString(2));
			returndata.add(cur.getString(3));
			returndata.add(cur.getString(4));
		}
		return returndata;
	}
	public static List<Map<String, String>> getrecord(Database database,String time_sign) {//"time_sign="+time_sign
		SQLiteDatabase db = database.getWritableDatabase();
		String[] parms={time_sign};
		Cursor cur = db.query("record",
				new String[] {"money","time","card_number","time_sign","save_states"},"time_sign=?",parms, null,
				null, null);
		List<Map<String, String>> recordlistdata = new ArrayList<Map<String, String>>();
		HashMap<String, String> recorddata;
		for (cur.moveToFirst(); !cur.isAfterLast(); cur.moveToNext()) {

			recorddata = new HashMap<String, String>();
			recorddata.put("money",cur.getString(0));
			recorddata.put("time",cur.getString(1));
			recorddata.put("card_number", cur.getString(2));
			recorddata.put("time_sign",cur.getString(3));
			recorddata.put("save_states", cur.getString(4));
			recordlistdata.add(0,recorddata);
		}
		return recordlistdata;
	}
	public void clearFeedTable() {
		String sql = "delete from member";
		SQLiteDatabase db = database.getWritableDatabase();
		db.execSQL(sql);
		// revertSeq(shujuku);

	}
	private void revertSeq(Database shujuku) {
		String sql = "update sqlite_sequence set seq=0 where name=music";
		SQLiteDatabase db = shujuku.getWritableDatabase();
		db.execSQL(sql);

	}

}